<?php



class BDS{
    protected static $tablename = false;
    protected static $db = false;

    public static final function Init($tablename = false) {
        self::$tablename = $tablename;
        self::$db = &$GLOBALS['MAIN']->DB;        
    }
    
    public static final function Info($param = false, $tablename = false) {
        $t = $tablename ? $tablename : static::$tablename;
        $sql = "show table status like '$t'";
        $result = self::$db->native($sql)->exec();
        $data = $result->fetch();
        if($param){
            $data = $data[$param];
        }
        return $data;
    }
    
    public static final function Fields($tablename = false) {
        $t = $tablename ? $tablename : static::$tablename;
        $sql = "show fields from $t";
        $result = self::$db->native($sql)->exec();
        $data = $result->GetList();        
        return $data;
    }

    public static function Get($id, $fields = "*"){
        $process = self::$db->select()->table(static::$tablename)->fields($fields)->cond('id', $id)->exec();
        return $process->fetch();
    }
    
    public static function GetList($filter = array(), $sort = array(), $fields = "*", $limit = false){
        $process = self::$db->select()->table(static::$tablename)->fields($fields);
        foreach($filter as $logic_field => $cond_value){
            $logic = DB_LOGIC_AND;
            $cond = DB_COND_EQUAL;
            if(preg_match("#^(\d+):(.+)$#", $logic_field, $matches)){
                $logic = $matches[1];
                $field = $matches[2];
            }else{
                $field = $logic_field;
            }
            if(preg_match("#^(\d+):(.+)$#", $cond_value, $matches)){
                $cond = $matches[1];
                $value = $matches[2];                
            }else{
                $value = $cond_value;
            }
            
            $process->cond($field, $value, $cond, $logic);
        }
        foreach($sort as $sort=>$direct){
            $process->order($sort, $direct);
        }
        if($limit){
            $process->limit($limit);
        }        
        $result = $process->exec();
        return $result;
    }
    
    public static function Create($data, $additional_values = false){
        $process = self::$db->insert()->table(static::$tablename)->fields(array_keys($data))->values($data)->exec();
        return $process;
    }
    
    public static function CreateList(){
        $data = func_get_args();
        $keys = array_keys($data[0]);
        $process = self::$db->insert()->table(static::$tablename)->fields($keys);
        foreach($data as $params){
            $process->values($params);
        }
        $result = $process->exec();
        return $result;
    }
    
    public static function Delete($id){
        $process = self::$db->delete()->table(static::$tablename)->cond('id', $id)->exec();
        return $process;
    }
    
    public static function DeleteList($ids){
        $process = self::$db->delete()->table(static::$tablename)->cond('id', "(".implode(",",$ids).")", DB_COND_IN)->noquotes()->exec();
        return $process;
    }
    
    public static function Update($id, $data){
        $process = self::$db->update()->table(static::$tablename)->cond('id', $id);
        foreach ($data as $field => $value){
            $process->set($field, $value);
        }
        $result = $process->exec();
        return $result;
    }
    
    public static function UpdateList($filter, $data){
        $process = self::$db->update()->table(static::$tablename);
        foreach($filter as $logic_field => $cond_value){
            $logic = DB_LOGIC_AND;
            $cond = DB_COND_EQUAL;
            if(preg_match("#^(\d+):(.+)$#", $logic_field, $matches)){
                $logic = $matches[1];
                $field = $matches[2];
            }else{
                $field = $logic_field;
            }
            if(preg_match("#^(\d+):(.+)$#", $cond_value, $matches)){
                $cond = $matches[1];
                $value = $matches[2];
            }else{
                $value = $cond_value;
            }
            
            $process->cond($field, $value, $cond, $logic);
        }
        foreach ($data as $field => $value){
            $process->set($field, $value);
        }
        $result = $process->exec();
        return $result; 
    }

}